import { database } from '@app/constants';
import { Inject, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm';

@Injectable()
export class DatabaseService implements TypeOrmOptionsFactory {
  constructor(@Inject(ConfigService) private configService: ConfigService) {}
  createTypeOrmOptions(): TypeOrmModuleOptions | Promise<TypeOrmModuleOptions> {
    return {
      type: 'mysql',
      host: this.configService.getOrThrow(database.host),
      port: +this.configService.getOrThrow(database.port),
      username: this.configService.getOrThrow(database.username),
      password: this.configService.get(database.password),
      database: this.configService.getOrThrow(database.database),
      autoLoadEntities: true,
      synchronize: import.meta.env.DEV,
      logging: ['error'],
    };
  }
}
